package gui;

import dataconnect.Investment;
import dataconnect.SqlConnection;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;

/**
 * description: AllInvestment
 * 浏览全部项目
 * @author what-is-me
 * @date 2022/1/3 23:43
 */
public class AllInvestment extends JPanel {
    static half h1;
    static half h2;
    SqlConnection sql;

    public AllInvestment (SqlConnection sql) {
        /**
         * Description: AllInvestment
         * @param sql:dataconnect.SqlConnection
         * @return {@link null}
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:43
         */
        super(new BorderLayout());
        this.sql = sql;
        h1 = new half(true, sql);
        h2 = new half(false, sql);
        setLocation(1, 0);
    }

    public static void update () {
        /**
         * Description: update
         * 更新两个单独页面
         * @param :
         * @return
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:43
         */
        h1.update();
        h2.update();
    }

    @Override
    public void setLocation (int page, int x) {
        /**
         * Description: setLocation
         * 重载无用的函数来达成页面切换的功能
         * @param page:int
         * @param x:int
         * @return
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:44
         */
        removeAll();
        add(page == 1 ? h1 : h2, BorderLayout.CENTER);
        JLabel title = new JLabel(page == 1 ? "已结束的项目" : "进行中的项目", JLabel.CENTER);
        add(title, BorderLayout.NORTH);
        title.setFont(new Font("宋体", Font.BOLD, 20));
        updateUI();
    }
}

class half extends GridBagPanel {
    /**
     * description: half
     * 单个浏览页面
     * @date 2022/1/3 23:25
     */
    String tag;
    DefaultTableModel tModel;
    boolean b;
    SqlConnection sql;

    public half (boolean b, SqlConnection sql) {
        /**
         * Description: half
         * @param b:boolean
         * @param sql:dataconnect.SqlConnection
         * @return {@link null}
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:26
         */
        this.b = b;
        this.sql = sql;
        Object[][] row;
        if (b) {
            row = sql.inv.queryBeforeNow();
            tag = "已结束的项目";
        } else {
            row = sql.inv.queryAfterNow();
            tag = "进行中的项目";
        }
        tModel = row == null ? new DefaultTableModel() : new DefaultTableModel(row, Investment.tag);
        JTable table = new JTable(tModel);
        table.setFont(new Font("宋体", Font.PLAIN, 16));
        table.getTableHeader().setFont(new Font("宋体", Font.PLAIN, 16));
        table.setRowHeight(20);
        table.setPreferredScrollableViewportSize(new Dimension(600, 600));
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, 0, 0, 1, 1, 1, 0);
    }

    void update () {
        /**
         * Description: update
         * 替换两种页面
         * @param :
         * @return
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:26
         */
        Object[][] row;
        if (b) {
            row = sql.inv.queryBeforeNow();
            tag = "已结束的项目";
        } else {
            row = sql.inv.queryAfterNow();
            tag = "进行中的项目";
        }
        tModel.setDataVector(row, Investment.tag);
    }
}

